package com.qst.rongxiaoserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.rongxiaoserver.common.Result;
import com.qst.rongxiaoserver.model.ExpertModel;
import com.qst.rongxiaoserver.service.IExpertService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/expert")
@Tag(name = "专家管理")
public class ExpertController {

    @Autowired
    IExpertService expertService;

    @PostMapping("/add")
    @Operation(summary = "添加专家", description = "添加专家(服务层实现)")
    public Result addExpert(@Valid @RequestBody ExpertModel expertModel) {
        boolean res =  expertService.save(expertModel);
        return res ? Result.success("添加成功") : Result.fail("添加失败");

    }

    @PostMapping("/update")
    @Operation(summary = "修改专家", description = "修改专家(服务层实现)")
    public Result updateExpert(@Valid @RequestBody ExpertModel expertModel) {
        boolean res =  expertService.updateById(expertModel);
        return res? Result.success("修改成功") : Result.fail("修改失败");
    }

    // 删除专家
    @DeleteMapping("/delete")
    @Operation(summary = "删除专家", description = "删除专家(服务层实现)")
    public Result deleteExpert(@Valid @RequestParam Integer id) {
        boolean res =  expertService.removeById(id);
        return res? Result.success("删除成功") : Result.fail("删除失败");
    }

    // 获取查询详情
    @GetMapping("/get")
    @Operation(summary = "获取专家详情", description = "获取专家详情(服务层实现)")
    public Result getExpert(@Valid @RequestParam Integer id) {
        // 这里获取详情为啥是null
        ExpertModel expertModel = expertService.getById(id);
        return Result.success(expertModel);
    }

    // 查询所有专家
    @GetMapping("/list")
    @Operation(summary = "查询所有专家", description = "查询所有专家(服务层实现)")
    public Result listExpert(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             @RequestParam(required = false) String expertName) {
        Page<ExpertModel> datas =  expertService.listExpert(pageNum,pageSize, expertName);
        return Result.success(datas);

    }
}
